<?php
session_start();
require_once("../Structure/fonctions.php");

$PAGE_TITLE = "Moderation - Lolgames";
$CONTENT_INCLUDE = "moderation.html";

if (isset($_SESSION["user_id"])) {
    $bdd = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_BASE);
	
	include("../Structure/updateUser.php");
	
	if(isset($_POST['blockImg']))
	{
	
		$query = "UPDATE images
				SET statusID = 3
				WHERE id = ".$_POST['id']." ;";
				
		$res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
	}

    //------------------------------------------------------------------//
    //      Tout les utilisateurs	
    //------------------------------------------------------------------//

	$sortAU = "id";
	$orderAU = "ASC";
	
	if(isset($_POST['allUsersSortPost']))
	{
		$sortAU = $_POST['sort'];
		$orderAU = $_POST['order'];
	}
	
    $query =
	"SELECT  u.id, roleID, u.statusID, avatarID, pseudo, lastName, firstName, email, pseudo, hiredate, i.link, i.statusID imgStatus
	FROM users u
	JOIN images i ON u.avatarID = i.id
	ORDER BY ".$sortAU." ".$orderAU." ;";
	$res_users = mysqli_query($bdd, $query) or die(mysqli_error($bdd));


    //------------------------------------------------------------------//
    //      Article en attente                                       
    //------------------------------------------------------------------//
    
	$sortWA = "id";
	$orderWA = "ASC";
	
	if(isset($_POST['waitArtsSortPost']))
	{
		$sortWA = $_POST['sort'];
		$orderWA = $_POST['order'];
	}
	
	$query =
	"SELECT a.id, a.type, a.autorID, imageID, title, date, a.statusID, i.link, u.lastName, u.firstName, u.pseudo as autor, i.statusID imgStatus
	FROM articles a 
	JOIN users u ON a.autorID = u.id
	JOIN images i ON a.imageID = i.id
	WHERE a.statusID = 0 
	ORDER BY ".$sortWA." ".$orderWA.";";
	
	$res_standByArticles = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

	//------------------------------------------------------------------//
    //              Tout les commentaires                                      
    //------------------------------------------------------------------//
	
	$sortAC = "date";
	$orderAC = "DESC";
	
	if(isset($_POST['allCommsSortPost']))
	{
		$sortAC = $_POST['sort'];
		$orderAC = $_POST['order'];
	}
	
	$query =
	"SELECT c.id, c.autorID, articleID, c.content, c.date as date, c.statusID, c.note, u.lastName, u.firstName, u.pseudo autor, a.title
	FROM comments c
	JOIN users u ON c.autorID = u.ID
	JOIN articles a ON c.articleID = a.id
	ORDER BY ".$sortAC." ".$orderAC.";";
	$res_allComments = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

	//------------------------------------------------------------------//
    //      Toutes les images                                          
    //------------------------------------------------------------------//
	$query =
	"SELECT i.id, autorID, link, i.statusID, u.lastName, u.firstName, u.pseudo
	FROM images i
	JOIN users u ON i.autorID = u.ID
	WHERE i.statusID != 3;";
	$res_allImages = mysqli_query($bdd, $query)or die(mysqli_error($bdd));

	//------------------------------------------------------------------//
    //      Tout les articles                              
    //------------------------------------------------------------------//
	
	$sortAA = "date";
	$orderAA = "DESC";
	
	if(isset($_POST['allArtsSortPost']))
	{
		$sortAA = $_POST['sort'];
		$orderAA = $_POST['order'];
	}
	
	$query =
	"SELECT a.id, a.type, a.autorID, a.content, imageID, title, date, a.statusID, i.link, u.lastName, u.firstName, u.pseudo as autor, i.statusID imgStatus
	FROM articles a 
	JOIN users u ON a.autorID = u.ID
	JOIN images i ON a.imageID = i.ID
	ORDER BY ".$sortAA." ".$orderAA.";";
	
	$res_allArticles = mysqli_query($bdd, $query)or die(mysqli_error($bdd));
}

include_once("../Structure/structure.php");

?>